import Cookies from "js-cookie";

let util = {};
util.getDifferDay = function (n, d) { //与date相差n的日期 默认今天
    let date = new Date();
    if (d) date = new Date(d);
    date.setTime(date.getTime() + 24 * 60 * 60 * 1000 * n);
    let year = date.getFullYear();
    let month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1));
    let day = date.getDate() > 9 ? date.getDate() : ('0' + date.getDate());
    return year + "-" + month + "-" + day;
}

util.compare = function (property, type) { //对象根据某个字段排序
    return function (a, b) {
        let value1 = a[property] ? a[property] : 0;
        let value2 = b[property] ? b[property] : 0;
        if (/[\u4E00-\u9FA5]/g.test(value1)) {
            if (type == 'asc') { //小到大
                return value1.localeCompare(value2)
            } else {
                return value2.localeCompare(value1)
            }
        } else if (/^[0-9]+$/g.test(value1) && /^[0-9]+$/g.test(value2)) {
            if (type == 'asc') { //小到大
                return parseInt(value1) < parseInt(value2)
            } else {
                return parseInt(value2) < parseInt(value1)
            }
        } else {
            return value1 < value2
        }
    }
}

util.compare1 = function (property, type) { //对象根据某个字段排序
    return function (a, b) {
        let value1 = a[property] ? a[property] : 0;
        let value2 = b[property] ? b[property] : 0;
        if (type == 'asc') { //小到大
            return parseInt(value1) - parseInt(value2)
        } else {
            return parseInt(value2) - parseInt(value1)
        }
    }
}

util.deepClone = function (obj) { //深度克隆
    var result;
    var oClass = isClass(obj);
    if (oClass === "Object") {
        result = {};
    } else if (oClass === "Array") {
        result = [];
    } else {
        return obj;
    }
    for (var key in obj) {
        var copy = obj[key];
        if (isClass(copy) == "Object") {
            result[key] = arguments.callee(copy); //递归调用
        } else if (isClass(copy) == "Array") {
            result[key] = arguments.callee(copy);
        } else {
            result[key] = obj[key];
        }
    }
    return result;
}

function isClass(o) {
    if (o === null) return "Null";
    if (o === undefined) return "Undefined";
    return Object.prototype.toString.call(o).slice(8, -1);
}

util.loginOver = function () { //登录过期
    this.$store.dispatch("handleUserInfoChange", null);
    this.$store.dispatch("handleLoginTypeChange", 1);
    this.$store.dispatch("handleLoginBoxChange", true);
    Cookies.remove("userinfo");
    alert("请先登录");
}

util.timestampToTime = function (timestamp) {
    var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    return Y + M + D;
    // var h = date.getHours() + ':';
    // var m = date.getMinutes() + ':';
    // var s = date.getSeconds();
    // return Y + M + D + h + m + s;
}

export default util;